import 'package:ac/api/ac_urls.dart';
import 'package:ac/common/ac_pv_manager.dart';
import 'package:ac/data/ac_app_preferences.dart';
import 'package:ac/data/ac_user_manager.dart';
import 'package:ac/page/ac_message/ac_message_logic.dart';
import 'package:ac/route/ac_route_pages.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'ac_setting_state.dart';

class AcSettingLogic extends GetxController {
  final AcSettingState state = AcSettingState();
  final autoTranslate = false.obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    autoTranslate.value = AcAppPreferences.instance.getAutoTranslate();
  }

  void logout() async {
    var res = await AcUrls.logout.post();
    if (res?["code"] == 0) {
      LogPvManger.stat(page: LogPages.logoutPage);
      AcUserManager.logout();
      await Get.find<AcMessageLogic>().logout();
      Get.offAllNamed(AcRouteNames.login);
    }
  }

  void deleteAccount() async {
    Get.defaultDialog(
      title: "Delete Account",
      content: const Text("Are you sure you want to delete your account?"),
      textConfirm: "Confirm",
      textCancel: "Cancel",
      confirmTextColor: Colors.white,
      onConfirm: () async {
        var res = await AcUrls.deleteAccount.post();
        LogPvManger.stat(page: LogPages.deleteAccountPage);
        if (res?["code"] == 0) {
          AcUserManager.logout();
          Get.offAllNamed(AcRouteNames.login);
        }
      },
    );
  }

  void setAutoTranslate() {
    autoTranslate.value = !autoTranslate.value;
    AcAppPreferences.instance.setAutoTranslate(autoTranslate.value);
  }
}
